package com.dong.Arrays.arithemtic;
import java.util.Arrays;

/**
 *  移除元素
 */
public class Demo3 {
    public static void main(String[] args) {
        int [] nums = {0,1,2,2,3,0,4,2};
        int val = 2;
        Demo3 demo3 = new Demo3();
        int len = demo3.removeElement(nums, val);
        System.out.println(Arrays.toString(nums));//[0, 1, 3, 0, 4, 0, 4, 2]

    }

    public int removeElement(int[] nums, int val) {
        int n = nums.length;
        int left = 0;
        for (int right = 0; right < n; right++) {
            if (nums[right] != val) {
                nums[left] = nums[right];
                left++;
            }
        }
        return left;

    }

}
